﻿using aier.dataaccess.model;
using Dapper;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
   public class CompetitorDal
    {
        public DapperHelper dapperHelper { get; set; }

        public ILog log { get; set; }

        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/社区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitor
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        public List<CompetitorView> QueryCompetitor(CompetitorView competitorView)
        {
            List<CompetitorView> competitorViews = null;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select * from AIER_COMPETITOR_INFO s /**where**/ ";
                var sqlTemplate = sqlBuilder.AddTemplate(sql);

                if (competitorView.competitorid != Guid.Empty)
                {
                    sqlBuilder.Where(" s.competitorid=@competitorid ");
                    sqlParams.Add("@competitorid", competitorView.competitorid);
                }
                if (competitorView.new_province != null)
                {
                    sqlBuilder.Where(" s.new_province=@new_province ");
                    sqlParams.Add("@new_province", competitorView.new_province);
                }
                if (competitorView.new_market != null)
                {
                    sqlBuilder.Where(" s.new_market=@new_market ");
                    sqlParams.Add("@new_market", competitorView.new_market);
                }
                if (competitorView.new_districtcounty != null)
                {
                    sqlBuilder.Where(" s.new_districtcounty=@new_districtcounty ");
                    sqlParams.Add("@new_districtcounty", competitorView.new_districtcounty);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.name))
                {
                    //sqlBuilder.Where(" s.name=@name ");
                    //sqlParams.Add("@name", competitorView.name);
                    sqlBuilder.Where(" charindex(@name,s.name)>0 ");
                    sqlParams.Add("@name", competitorView.name);
                }
                if (competitorView.new_community != null)
                {
                    sqlBuilder.Where(" s.new_community=@new_community ");
                    sqlParams.Add("@new_community", competitorView.new_community);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_provincename))
                {
                    sqlBuilder.Where(" s.new_provincename=@new_provincename ");
                    sqlParams.Add("@new_provincename", competitorView.new_provincename);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_marketname))
                {
                    sqlBuilder.Where(" s.new_marketname=@new_marketname ");
                    sqlParams.Add("@new_marketname", competitorView.new_marketname);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_districtcountyname))
                {
                    sqlBuilder.Where(" s.new_districtcountyname=@new_districtcountyname ");
                    sqlParams.Add("@new_districtcountyname", competitorView.new_districtcountyname);
                }
                log.Info(string.Format("CompetitorDal对像中,QueryCompetitor ,SQL：'{0}' 参数：{1}", sqlTemplate.RawSql, sqlParams.ParametersToString(sqlTemplate.RawSql)));
                competitorViews = a.Query<CompetitorView>(sqlTemplate.RawSql, sqlParams).ToList();
            });

            return competitorViews;
        }
        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitorCount
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        public int QueryCompetitorCount(CompetitorView competitorView)
        {
            int count = 0;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select count(*) from AIER_COMPETITOR_INFO s /**where**/ ";
                var sqlTemplate = sqlBuilder.AddTemplate(sql);

                if (competitorView.competitorid != Guid.Empty)
                {
                    sqlBuilder.Where(" s.competitorid=@competitorid ");
                    sqlParams.Add("@competitorid", competitorView.competitorid);
                }
                if (competitorView.new_province != Guid.Empty)
                {
                    sqlBuilder.Where(" s.new_province=@new_province ");
                    sqlParams.Add("@new_province", competitorView.new_province);
                }
                if (competitorView.new_market != Guid.Empty)
                {
                    sqlBuilder.Where(" s.new_market=@new_market ");
                    sqlParams.Add("@new_market", competitorView.new_market);
                }
                if (competitorView.new_districtcounty != Guid.Empty)
                {
                    sqlBuilder.Where(" s.new_districtcounty=@new_districtcounty ");
                    sqlParams.Add("@new_districtcounty", competitorView.new_districtcounty);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.name))
                {
                    sqlBuilder.Where(" s.name=@name ");
                    sqlParams.Add("@name", competitorView.name);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_provincename))
                {
                    sqlBuilder.Where(" s.new_provincename=@new_provincename ");
                    sqlParams.Add("@new_provincename", competitorView.new_provincename);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_marketname))
                {
                    sqlBuilder.Where(" s.new_marketname=@new_marketname ");
                    sqlParams.Add("@new_marketname", competitorView.new_marketname);
                }
                if (!string.IsNullOrWhiteSpace(competitorView.new_districtcountyname))
                {
                    sqlBuilder.Where(" s.new_districtcountyname=@new_districtcountyname ");
                    sqlParams.Add("@new_districtcountyname", competitorView.new_districtcountyname);
                }

                log.Info(string.Format("CompetitorDal对像中,QueryCompetitor ,SQL：'{0}' 参数：{1}", sqlTemplate.RawSql, sqlParams.ParametersToString(sqlTemplate.RawSql)));
                count = a.ExecuteScalar<int>(sqlTemplate.RawSql, sqlParams);
            });

            return count;
        }
    }
}
